package com.cnbbx.office.Util;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.ConnectException;
import java.util.Random;

@Component
public class DocumentUtils {

    /**
     * 使用 JODConverter 将 Office 文档转换成 PDF 文件
     *
     * @param inFilePath  Office 文档路径
     * @param outFilePath 生成的 PDF 文件路径
     * @throws IOException
     */
    public static void convertOfficeDocumentToPDF(String inFilePath, String outFilePath)
            throws IOException {
        File inFile = new File(inFilePath);
        File outFile = new File(outFilePath);

        if (!inFile.exists()) {
            throw new FileNotFoundException("The file does not exist: " + inFile.getAbsolutePath());
        }

        OpenOfficeConnection connection = null;
        try {
            String[] doc = {"192.168.0.182", "192.168.0.179", "192.168.0.6", "192.168.0.22"};
            Random rand = new Random();
            int num = rand.nextInt(doc.length);
            String random = doc[num];
            System.out.println("link server: " + random);
            // connect to an OpenOffice.org instance running on port 8100
            connection = new SocketOpenOfficeConnection(random, 8100);
            connection.connect();
            // convert
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(inFile, outFile);
        } finally {
            // close the connection
            if (connection != null && connection.isConnected()) {
                connection.disconnect();
            }
        }

    }

    /**
     * 使用 JODConverter 将 Office 文档输入流转换成 PDF 输出流
     *
     * @param inputStream  输入流
     * @param inputFormat  输入流的文档格式
     * @param outputStream 输出流
     * @param outputFormat 输出流的文档格式
     * @throws IOException
     */
    public static void convert(InputStream inputStream, DocumentFormat inputFormat, OutputStream outputStream, DocumentFormat outputFormat) throws IOException {
        OpenOfficeConnection connection = null;
        try {
            String[] doc = {"192.168.0.182", "192.168.0.179", "192.168.0.6", "192.168.0.22"};
            Random rand = new Random();
            int num = rand.nextInt(doc.length);
            String random = doc[num];
            System.out.println("link server: " + random);
            connection = new SocketOpenOfficeConnection(random, 8100);
            connection.connect();
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(inputStream, inputFormat, outputStream, outputFormat);
        } catch (ConnectException e) {
            e.printStackTrace();
            throw new RuntimeException("OpenOffice服务启动失败！");
        } finally {
            if (connection != null && connection.isConnected()) {
                connection.disconnect();
            }
        }
    }

}

